package cn.iocoder.yudao.module.picturebook.controller.admin.classcheckin;

import cn.iocoder.yudao.module.picturebook.service.concern.ConcernService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.classcheckin.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.classcheckin.ClassCheckinDO;
import cn.iocoder.yudao.module.picturebook.service.classcheckin.ClassCheckinService;

@Tag(name = "绘本系统 - 班级打卡记录")
@RestController
@RequestMapping("/picturebook/class-checkin")
@Validated
public class ClassCheckinController {

    @Resource
    private ClassCheckinService classCheckinService;
    @Resource
    private ConcernService concernService;

    @PostMapping("/create")
    @Operation(summary = "创建班级打卡记录")
    @PreAuthorize("@ss.hasPermission('picturebook:class-checkin:create')")
    public CommonResult<Long> createClassCheckin(@Valid @RequestBody ClassCheckinSaveReqVO createReqVO, @RequestHeader("token") String userId) {
        createReqVO.setUserId(Long.parseLong(userId));
        return success(classCheckinService.createClassCheckin(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级打卡记录(无该功能)")
    @PreAuthorize("@ss.hasPermission('picturebook:class-checkin:update')")
    public CommonResult<Boolean> updateClassCheckin(@Valid @RequestBody ClassCheckinSaveReqVO updateReqVO) {
        classCheckinService.updateClassCheckin(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级打卡记录(无该功能)")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:class-checkin:delete')")
    public CommonResult<Boolean> deleteClassCheckin(@RequestParam("id") Long id) {
        classCheckinService.deleteClassCheckin(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除班级打卡记录(无该功能)")
    @PreAuthorize("@ss.hasPermission('picturebook:class-checkin:delete')")
    public CommonResult<Boolean> deleteClassCheckinList(@RequestParam("ids") List<Long> ids) {
        classCheckinService.deleteClassCheckinListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级打卡记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:class-checkin:query')")
    public CommonResult<ClassCheckinRespVO> getClassCheckin(@RequestParam("id") Long id) {
        ClassCheckinDO classCheckin = classCheckinService.getClassCheckin(id);
        return success(BeanUtils.toBean(classCheckin, ClassCheckinRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级打卡记录分页")
    @PreAuthorize("@ss.hasPermission('picturebook:class-checkin:query')")
    public CommonResult<PageResult<ClassCheckinRespVO>> getClassCheckinPage(@Valid ClassCheckinPageReqVO pageReqVO, @RequestHeader("token") String userId) {
        pageReqVO.setUserId(Long.parseLong(userId));
        PageResult<ClassCheckinRespVO> pageResult = classCheckinService.getClassCheckinPage(pageReqVO);

        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级打卡记录 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:class-checkin:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassCheckinExcel(@Valid ClassCheckinPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassCheckinRespVO> list = classCheckinService.getClassCheckinPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级打卡记录.xls", "数据", ClassCheckinRespVO.class,
                BeanUtils.toBean(list, ClassCheckinRespVO.class));
    }

    //获取所有的打卡记录
    @GetMapping("/list-all")
    @Operation(summary = "获取所有的打卡记录(分页)")
    public CommonResult<PageResult<CheckinRespAllData>> listAll(@Validated ClassCheckinPageReqVO pageReqVO,@RequestHeader("token") Long userId) {
        return CommonResult.success(classCheckinService.listAll(pageReqVO,userId));
    }

    //获取个人的打卡数据
    @GetMapping("/list-personal")
    @Operation(summary = "获取个人的打卡数据(分页)")
    public CommonResult<PageResult<CheckinRespAllData>> listPersonal(@Validated ClassCheckinPageReqVO pageReqVO, @RequestHeader("token") Long userId) {
        pageReqVO.setUserId(userId);
        return CommonResult.success(classCheckinService.listAll(pageReqVO,userId));
    }

    //获取关注的用户的打卡数据
    @GetMapping("/list-concern")
    @Operation(summary = "获取关注的用户的打卡数据(分页)")
    public CommonResult<PageResult<CheckinRespAllData>> listConcern(@Validated ClassCheckinPageReqVO pageReqVO, @RequestHeader("token") Long userId) {
        return CommonResult.success(classCheckinService.listAllByConcern(pageReqVO,userId));
    }
    //关注用户
    @PostMapping("/concern")
    @Operation(summary = "关注用户")
    public CommonResult<Boolean> concern(@RequestParam("concernId") Long concernId, @RequestHeader("token") Long userId) {
        return CommonResult.success(concernService.concern(concernId,userId));
    }
    //取消关注
    @DeleteMapping("/cancel-concern")
    @Operation(summary = "取消关注")
    public CommonResult<Boolean> cancelConcern(@RequestParam("concernId") Long concernId, @RequestHeader("token") Long userId) {
        return CommonResult.success(concernService.cancelConcern(concernId,userId));
    }
    //判断是否可以打卡
    @GetMapping("/check-can-checkin")
    @Operation(summary = "判断是否可以打卡")
    public CommonResult<Boolean> checkCanCheckin(@RequestHeader("token") Long userId) {
        return CommonResult.success(classCheckinService.checkCanCheckin(userId));
    }
    //班级内的打卡信息
    @GetMapping("/list-class-checkin")
    @Operation(summary = "班级内的打卡信息")
    public CommonResult<CheckinClassRespVO> listClassCheckin(@Validated ClassCheckinPageReqVO pageReqVO) {
        return CommonResult.success(classCheckinService.listAllClassCheckin(pageReqVO));
    }
    //获取用户打卡日历
    @GetMapping("/get-user-checkin-calendar")
    @Operation(summary = "获取用户打卡日历")
    public CommonResult<CheckinCalendarVO> getUserCheckinCalendar(@RequestHeader("token") Long userId) {
        return CommonResult.success(classCheckinService.getUserCheckinCalendar(userId));
    }
}